﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace AppFuscator
{
    class Utils
    {
        public const string MainHost = "appfuscator.com";
        public const string CabinetHost = "cabinet.appfuscator.com";

        public static string InternalFilesFolder
        {
            get
            {
                var s = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                    "66Bit",
                    "Appfuscator"
                );
                if (!Directory.Exists(s))
                    Directory.CreateDirectory(s);
                return s;
            }
        }

        public static string GetMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
                sBuilder.Append(data[i].ToString("x2"));

            return sBuilder.ToString();
        }

        public static string GetTextFromApi(string inp)
        {
            
            try
            {
                var root = JObject.Parse(inp);
                return root.Property("text").Value.ToString();
            }
            catch
            {
                return null;
            }
        }

        public static JObject ParseJson(string data)
        {
            try
            {
                return JObject.Parse(data);
            }
            catch
            {
                return null;
            }
        }

        public static string SerializeObject(object value)
        {
            var serializer = JsonSerializer.Create(null);

            var stringWriter = new StringWriter();

            using (var jsonWriter = new JsonTextWriter(stringWriter))
            {
                jsonWriter.QuoteName = false;
                jsonWriter.Formatting = Formatting.Indented;
                serializer.Serialize(jsonWriter, value);

                return stringWriter.ToString();
            }
        }

        public static string EncodeByMyAlgo(string data)
        {
            return Convert.ToBase64String(Encoding.UTF7.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(data))));
        }

        public static T DeserializeObject<T>(string value)
        {
            var serializer = JsonSerializer.Create(null);

            var stringReader = new StringReader(value);

            using (var reader = new JsonTextReader(stringReader))
            {
                return serializer.Deserialize<T>(reader);
            }
        }

        public static string MakeRelativePath(string fromPath, string toPath)
        {
            if (!fromPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
                fromPath += Path.DirectorySeparatorChar;

            if (string.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath");
            if (string.IsNullOrEmpty(toPath)) throw new ArgumentNullException("toPath");

            Uri fromUri = new Uri(fromPath);
            Uri toUri = new Uri(toPath);

            Uri relativeUri = fromUri.MakeRelativeUri(toUri);
            string relativePath = Uri.UnescapeDataString(relativeUri.ToString());

            return relativePath.Replace('/', Path.DirectorySeparatorChar);
        }

        internal static string CntCase(int cnt, string case1, string case2)
        {
            if (cnt > 1) 
                return case2;
            else
                return case1;
        }

        public static int CompareByExtension(string x, string y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (y == null)
                {
                    return 1;
                }
                else
                {
                    var xe = Path.GetExtension(x);
                    var ye = Path.GetExtension(y);
                    int retval = xe.CompareTo(ye);
                    return retval;
                }
            }
        }
    }
}
